<template>
	<div id="search">
		<!-- 搜索栏 -->
		<div class="header-title">
			<input class="searchInput" placeholder="请输入要搜索的关键字" type="text" v-model="form.kw">
			<img src="@/assets/icon/search.png" @click="searchKeyword">
		</div>
		<van-empty image="search" v-if="!dataList.length" description="没有搜索到你想要的内容" />
		<div class="contentBody">
			<!-- 搜索商品展示信息 -->
			<div class="searchGoods" v-if="dataList.length">
				<div class="dataCount">本次检索一共查找到了{{dataList.length}}条数据</div>
				<div class="good-item" v-for="itemMap in dataList" :key="itemMap.id">
					<div>名称：<span v-html="itemMap.foodName"></span></div>
					<div>类别：<span v-html="itemMap.category"></span></div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import { mapActions } from 'vuex'
	export default {
		data() {
			return {
				form: {
					kw:""
				},
				dataList:[]
			}
		},
		methods:{
			...mapActions(["searchKeywords"]),
			// 搜索关键词
			async searchKeyword(){
				const reg=new RegExp(this.form.kw,"g")
				const results=await this.searchKeywords(this.form)
				results.forEach(itemMap=>{
					itemMap.category=itemMap.category.replace(reg,`<span style="color:#f00">${this.form.kw}</span>`)
					itemMap.foodName=itemMap.foodName.replace(reg,`<span style="color:#f00">${this.form.kw}</span>`)
				})
				this.dataList=results
			}
		}
	}
</script>

<style lang="scss" scoped>
@import "../../scss/GLOBAL.scss";
#search {
	.header-title {
		position: fixed;
		height: 2.88em;
		width: 100%;
		top: 0;
		background-color: $themeColor;
		display: flex;
		align-items: center;
		.searchInput {
			border-radius: 50px;
			width:80%;
			display: block;
			margin: 0 auto 0 10px;
			padding: 5px 20px;
			font-size: .8em;
		}
		img {
			width: 1.88em;
			margin-right: 1.25em;
		}
	}
	.contentBody {
		padding: 0 10px;
		.searchGoods {
			margin-top: 2.88em;
			.dataCount {
				padding: 5px 0;
			}
			.good-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: .3em 0;
			}
		}
	}
	
}
</style>